package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.UnitOfMeasurementService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.masterData.UnitOfMeasurementDelDto;
import com.ccys.db.dto.masterData.UnitOfMeasurementPageQueryDto;
import com.ccys.db.dto.masterData.UnitOfMeasurementSaveDto;
import com.ccys.db.entity.TblUnitOfMeasurement;
import com.ccys.db.mapper.TblUnitOfMeasurementMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.masterData.UnitOfMeasurementPageQueryVo;
import com.ccys.db.vo.web.productAndService.IdNameVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 计量管理实现类
 * @author: ZhangJingCan
 * @create: 2023-12-12 16:22
 **/

@Service
public class UnitOfMeasurementServiceImpl extends BaseServiceImpl<TblUnitOfMeasurement> implements UnitOfMeasurementService {

    @Resource
    private TblUnitOfMeasurementMapper tblUnitOfMeasurementMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult save(UnitOfMeasurementSaveDto dto) {
        //校验 - 集团ID存在并且name存在 - 需要查询数据库是否存在相同数据 - 并且是未删除状态的
        if (ObjectUtil.isNotEmpty(dto.getGroupId()) && ObjectUtil.isNotEmpty(dto.getName())) {
            //准备条件
            Example example = new Example(TblUnitOfMeasurement.class);
            example.createCriteria()
                    .andEqualTo("groupId", dto.getGroupId())
                    .andEqualTo("name", dto.getName())
                    .andEqualTo("delFlag", 0);
            //判断ID是否有值 - 有值为修改 - 修改需要排除当前自身
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                example.createCriteria().andNotEqualTo("id", dto.getId());
            }
            //开始查询并且判断 - 如果存在 - 返回错误信息
            if (ObjectUtil.isNotEmpty(tblUnitOfMeasurementMapper.selectOneByExample(example))) {
                return ResponseResult.error("There is data with the same name under the current group!");
            }
        }
        if (ObjectUtil.isNotEmpty(dto.getId()) && ObjectUtil.isNotEmpty(dto.getStatus())) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
        }

        //开始新增/修改 - 并且直接返回是否成功
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult delete(UnitOfMeasurementDelDto dto) {
        //校验ID是否存在值
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据软删除
        TblUnitOfMeasurement unit = new TblUnitOfMeasurement();
        unit.setId(dto.getId());
        unit.setDelFlag(1);
        if (tblUnitOfMeasurementMapper.updateByPrimaryKeySelective(unit) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult findPage(UnitOfMeasurementPageQueryDto dto) {
        //检查值是否在范围 - 不在则赋值为null
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        PageInfo<UnitOfMeasurementPageQueryVo> info = new PageInfo<>(tblUnitOfMeasurementMapper.findPage(dto));
        return ResponseResult.success(info);
    }

    @Override
    public ResponseResult getIdNameList() {
        String groupId = UserUtil.getCurLoginUserGroupId();
        List<IdNameVo> res = tblUnitOfMeasurementMapper.getIdNameList(groupId);
        return ResponseResult.success(res);
    }
}
